package com.redpacketcover.rc.common;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.redpacketcover.rc.R;
import com.redpacketcover.rc.base.BaseApplication;

public class ToastHelp {
    protected static long lastTime;

    public static void toast(String msg) {
        makeToast(BaseApplication.getInstance(), msg, null);
    }


    public static void toast(Context context, int msgId) {
        makeToast(context, msgId, null);
    }

    public static void toast(Context context, int msgId, Drawable icon) {
        makeToast(context, msgId, icon);
    }

    public static void toast(Context context, String msgId) {
        makeToast(context, msgId, null);
    }

    public static void toast(Context context, String msgId, Drawable icon) {
        makeToast(context, msgId, icon);
    }

    public static void makeToast(Context context, int msgId, Drawable icon) {
        if (!isShouldShow(context)) return;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.toast_layout, null);
        TextView toastMsg = view.findViewById(R.id.text);
        ImageView toastIcon = view.findViewById(R.id.toast_icon);
        if (icon != null) {
            toastIcon.setBackground(icon);
            toastIcon.setVisibility(View.VISIBLE);
        } else {
            toastIcon.setVisibility(View.GONE);
        }
        toastMsg.setText(msgId);
        Toast toast = new Toast(context);
        // toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    public static void makeToast(Context context, String msgId, Drawable icon) {
        if (!isShouldShow(context)) return;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.toast_layout, null);
        TextView toastMsg = view.findViewById(R.id.text);
        ImageView toastIcon = view.findViewById(R.id.toast_icon);
        if (icon != null) {
            toastIcon.setBackground(icon);
            toastIcon.setVisibility(View.VISIBLE);
        } else {
            toastIcon.setVisibility(View.GONE);
        }
        toastMsg.setText(msgId);
        Toast toast = new Toast(context);
        // toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }


    /**
     * 处理重复toast
     *
     * @param context
     * @return
     */
    protected static boolean isShouldShow(Context context) {
        long delay = System.currentTimeMillis() - lastTime;
        lastTime = System.currentTimeMillis();
        if (context == null) {
            return false;
        } else {
            return delay >= 1000;
        }

    }
}
